require("dplyr")
library("haven")

## Set working directory
## to Dataverse folder

load("Harris_Data/Harris 2011 Public Opinion Survey, study no. 40515/harris_40515.RData")
survey <- table
survey <- as.data.frame(survey)

# pid
survey$pid <- as.character(survey$id)

# study 
survey$study <- "40515"

# study year (year)
survey$year <- 2011

# geographic data (urban)
survey$urban <- as.character(as_factor(survey$Q1005))

# geographic data (region)
survey$region <- as.character(as_factor(survey$Q1015))

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
table(survey$Q606A2)
survey$inequality <- as.character(as_factor(survey$Q606A2))
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- NA
survey$union.other <- NA

# employment (employed)
survey$employed <- NA

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- NA

# occ self
survey$occupation.self <- NA

# household size (hhsize)
table(survey$Q368)
survey$hh_above18 <-  as.numeric(survey$Q368)
table(survey$hh_above18)
table(survey$Q372)
survey$hh_under18 <- as.numeric(survey$Q372)
survey$hhsize <- survey$hh_above18 + survey$hh_under18
table(survey$hhsize)

# education (educ)
table(survey$Q437)
survey$educ <- as.character(as_factor(survey$Q437))
  
# household income (income)
survey$income <- as.character(as_factor(survey$Q462))

# age
table(survey$Q280)
survey$age <- as.character(survey$Q280)

# race
table(survey$Q485)
survey$race <- as.character(as_factor(survey$Q485))

# politics (party)
table(survey$Q1500)
survey$party <- as.character(as_factor(survey$Q1500))

# politics (ideology)
table(survey$Q1520)
survey$ideology <- as.character(as_factor(survey$Q1520))

# gender
table(survey$Q268)
survey$gender <- as.character(as_factor(survey$Q268))

# religion
table(survey$Q1540)
survey$religion <- as.character(as_factor(survey$Q1540))

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(as_factor(survey$Q606A1))
survey$dontcount <- as.character(as_factor(survey$Q606A3))
survey$leftout <- as.character(as_factor(survey$Q606A4))

## quesiton place
survey$question_place <- "before party"

# subset
survey_40515 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                          "inequality", "inequality.variable", "union.self", "union.other",
                          "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                          "age", "race", "party", "ideology", "gender", "religion",
                          "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                          "question_place")]

# save file
#saveRDS(survey_40515, file = "Harris_Data/survey_40515.rds")
